package cn.xxstudy.notification

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import cn.xxstudy.notification.ui.login.LoginActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        send_notification_1.setOnClickListener {
            val channelId = "100"
            createChannel(channelId, "测试")
            val intent = Intent(this, LoginActivity::class.java)
            val pendingIntent = PendingIntent.getActivities(this, 0, arrayOf(intent), PendingIntent.FLAG_UPDATE_CURRENT)
            val notificationManager = NotificationManagerCompat.from(this)
            val notification = NotificationCompat
                .Builder(this, channelId)
                .setContentTitle("适配通知栏")
                    //单行内容
//                .setContentText("通知：请广大Android开发者尽快完成适配android7.0以上的通知栏适配工作！")
                    //多行内容
                .setStyle(NotificationCompat.BigTextStyle().bigText("通知：请广大Android开发者尽快完成适配android7.0以上的通知栏适配工作！"))
                .setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher))
                .setSmallIcon(R.mipmap.icon_notification)
                .setAutoCancel(false)
                .setContentIntent(pendingIntent)
                .build()
            notificationManager.notify(1, notification)
        }
    }

    private fun createChannel(id: String, name: String) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val notificationManager = NotificationManagerCompat.from(this)
            val notificationChannel = NotificationChannel(id, name, NotificationManager.IMPORTANCE_DEFAULT)
            notificationManager.createNotificationChannel(notificationChannel)
        }
    }

    fun v(vararg v:String){
        val l= arrayListOf(v)
    }
}
